package com.migu.contacts.login;

import static com.migu.contacts.net.Service.API;

import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;

import androidx.annotation.Nullable;

import com.blankj.utilcode.util.ToastUtils;
import com.frank.libandroid.BaseActivity;
import com.frank.libandroid.HttpCallback;
import com.frank.libandroid.util.AutoDeleteUtil;
import com.frank.libandroid.util.CountDownUtil;
import com.frank.libandroid.util.TextWatcherUtil;
import com.migu.contacts.BuildConfig;
import com.migu.contacts.MainActivity;
import com.migu.contacts.bean.LoginReq;
import com.migu.contacts.bean.LoginRes;
import com.migu.contacts.databinding.ActivityLoginBinding;
import com.migu.contacts.main.my.PrivacyActivity;
import com.migu.contacts.main.my.UserProtocolActivity;
import com.migu.contacts.util.UserCenter;
import com.umeng.socialize.UMAuthListener;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.bean.SHARE_MEDIA;

import java.util.Map;

public class LoginActivity extends BaseActivity<ActivityLoginBinding> {
    @Override
    protected void initView() {
        super.initView();
        AutoDeleteUtil.autoDelete(mBinding.phone, mBinding.phoneDelete);
        TextWatcherUtil.setTextWatchEnable(mBinding.btn, mBinding.phone, mBinding.codeInput);
    }

    @Override
    protected void initData() {
        super.initData();
        LoginReq bean = new LoginReq();
        bean.grantType = "sms_code";
        if (BuildConfig.DEBUG) {
            bean.username = "13938260436";
            mBinding.check.setSelected(true);
        }
        mBinding.setBean(bean);
    }

    public void login(View view) {
        Log.d(TAG, "login() called with: view = [" + view + "]");
        if (!mBinding.check.isSelected()) {
            ToastUtils.showShort("请阅读平台协议");
            return;
        }
        API.login(mBinding.getBean()).watch(this, new HttpCallback<LoginRes>() {
            @Override
            protected void onSuccess(LoginRes loginRes) {
                UserCenter.setLoginRes(loginRes);
                finish();
                Intent intent = new Intent(mActivity, MainActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                startActivity(intent);
            }
        });
    }

    public void getCode(View view) {
        Log.d(TAG, "getCode() called with: view = [" + view + "]");
        LoginReq bean = mBinding.getBean();
        if (TextUtils.isEmpty(bean.username)) {
            ToastUtils.showShort("请输入手机号");
            return;
        }
        CountDownUtil.startCountDown(mBinding.verifyCode, 60);
        API.sendSms(2, bean.username).watch(this, new HttpCallback<Boolean>() {
            @Override
            protected void onSuccess(Boolean aBoolean) {
                Log.d(TAG, "onSuccess() called with: aBoolean = [" + aBoolean + "]");
                if (aBoolean) {
                    ToastUtils.showShort("发送成功");
                    if (BuildConfig.DEBUG) {
                        mBinding.codeInput.setText("111111");
                    }
                }
            }
        });
    }

    public void passwordLogin(View view) {
        start(PasswordLoginActivity.class);
    }

    public void register(View view) {
        start(RegisterActivity.class);
    }

    public void wechat(View view) {
        UMShareAPI.get(mActivity).getPlatformInfo(mActivity, SHARE_MEDIA.WEIXIN, new UMAuthListener() {
            @Override
            public void onStart(SHARE_MEDIA share_media) {
                Log.d(TAG, "onStart() called with: share_media = [" + share_media + "]");
            }

            @Override
            public void onComplete(SHARE_MEDIA share_media, int i, Map<String, String> map) {
//                {country=,
//                unionid=oA5GK6A3rB5xU9L2KPQsuEqnNeW0,
//                gender=0, city=, openid=ogsvW5mlVFECbO1jRMmZMTZQUY3w,
//                language=,
//                profile_image_url=https://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTIZYvGRwW1OKN6z26as8bFTY7zS0e2SHrGzljjgdibrDrgicvlPGWgjuVqqlib5u7z4zbAj3It1HQDmw/132,
//                accessToken=60_pDAXFntyJBX30lLC8J_qZ0kjvw6eJ2Qh8DPEsUWr4So_rcaqAoEXPRFAV4-0ZDxzjsEviv29KqyUNej5Euu448sbX5gvxuAhrm6ZFpgUOmg,
//                access_token=60_pDAXFntyJBX30lLC8J_qZ0kjvw6eJ2Qh8DPEsUWr4So_rcaqAoEXPRFAV4-0ZDxzjsEviv29KqyUNej5Euu448sbX5gvxuAhrm6ZFpgUOmg,
//                uid=oA5GK6A3rB5xU9L2KPQsuEqnNeW0, province=, screen_name=Frank, name=Frank,
//                iconurl=https://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTIZYvGRwW1OKN6z26as8bFTY7zS0e2SHrGzljjgdibrDrgicvlPGWgjuVqqlib5u7z4zbAj3It1HQDmw/132,
//                expiration=1663221582115, expires_in=1663221582115,
//                refreshToken=60_iMslQYiPZLiXw8cBIFZvXmBqAyLZEM6cQlmW34xgdjbj3l5fnbqhiDquvorIT9HpPKPr26nJFi_7S63fkg6Ii8yu7LxS7dQb98sU2pdWdeA}
                Log.d(TAG, "onComplete() called with: share_media = [" + share_media + "], i = [" + i + "], map = [" + map + "]");
                LoginReq loginReq = new LoginReq();
                loginReq.grantType = "wechat";
                loginReq.authorizationCode = map.get("accessToken");
                API.login(loginReq).watch(mActivity, new HttpCallback<LoginRes>() {
                    @Override
                    protected void onSuccess(LoginRes loginRes) {
                        UserCenter.setLoginRes(loginRes);
                        finish();
                        Intent intent = new Intent(mActivity, MainActivity.class);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                        startActivity(intent);
                    }
                });
            }


            @Override
            public void onError(SHARE_MEDIA share_media, int i, Throwable throwable) {
                Log.d(TAG, "onError() called with: share_media = [" + share_media + "], i = [" + i + "], throwable = [" + throwable + "]");

            }

            @Override
            public void onCancel(SHARE_MEDIA share_media, int i) {
                Log.d(TAG, "onCancel() called with: share_media = [" + share_media + "], i = [" + i + "]");
            }
        });
    }

    public void qq(View view) {
        UMShareAPI.get(mActivity).getPlatformInfo(mActivity, SHARE_MEDIA.QQ, new UMAuthListener() {
            @Override
            public void onStart(SHARE_MEDIA share_media) {
                Log.d(TAG, "onStart() called with: share_media = [" + share_media + "]");
            }

            @Override
            public void onComplete(SHARE_MEDIA share_media, int i, Map<String, String> map) {
                Log.d(TAG, "onComplete() called with: share_media = [" + share_media + "], i = [" + i + "], map = [" + map + "]");
//                {msg=, ret=0, unionid=, gender=男, is_yellow_vip=0, city=深圳,
//                level=0, openid=F84E54922F3EA7CBAF08651D714BDFBB,
//                profile_image_url=http://thirdqq.qlogo.cn/g?b=oidb&k=h7SaGY81hDe4ia7ndQvibSdw&s=100&t=1486951677,
//                accessToken=6348F2382BCDFC232B1A46FD20336B4D, access_token=6348F2382BCDFC232B1A46FD20336B4D, uid=F84E54922F3EA7CBAF08651D714BDFBB,
//                is_yellow_year_vip=0, province=广东, screen_name=絮絮风飞, name=絮絮风飞,
//                iconurl=http://thirdqq.qlogo.cn/g?b=oidb&k=h7SaGY81hDe4ia7ndQvibSdw&s=100&t=1486951677,
//                yellow_vip_level=0, expiration=1670855391906, vip=0, expires_in=1670855391906}
                LoginReq req = new LoginReq();
                req.grantType = "qq";
                req.authorizationCode = map.get("openid");
                API.login(req).watch(mActivity, new HttpCallback<LoginRes>() {
                    @Override
                    protected void onSuccess(LoginRes loginRes) {
                        UserCenter.setLoginRes(loginRes);
                        finish();
                        Intent intent = new Intent(mActivity, MainActivity.class);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                        startActivity(intent);
                    }
                });
            }

            @Override
            public void onError(SHARE_MEDIA share_media, int i, Throwable throwable) {
                Log.d(TAG, "onError() called with: share_media = [" + share_media + "], i = [" + i + "], throwable = [" + throwable + "]");

            }

            @Override
            public void onCancel(SHARE_MEDIA share_media, int i) {
                Log.d(TAG, "onCancel() called with: share_media = [" + share_media + "], i = [" + i + "]");
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
    }

    public void weibo(View view) {
        Log.d(TAG, "weibo() called with: view = [" + view + "]");
        UMShareAPI.get(mActivity).getPlatformInfo(mActivity, SHARE_MEDIA.SINA, new UMAuthListener() {
            @Override
            public void onStart(SHARE_MEDIA share_media) {
                Log.d(TAG, "onStart() called with: share_media = [" + share_media + "]");
            }

            @Override
            public void onComplete(SHARE_MEDIA share_media, int i, Map<String, String> map) {
                Log.d(TAG, "onComplete() called with: share_media = [" + share_media + "], i = [" + i + "], map = [" + map + "]");
//                {msg=, ret=0, unionid=, gender=男, is_yellow_vip=0, city=深圳,
//                level=0, openid=F84E54922F3EA7CBAF08651D714BDFBB,
//                profile_image_url=http://thirdqq.qlogo.cn/g?b=oidb&k=h7SaGY81hDe4ia7ndQvibSdw&s=100&t=1486951677,
//                accessToken=6348F2382BCDFC232B1A46FD20336B4D, access_token=6348F2382BCDFC232B1A46FD20336B4D, uid=F84E54922F3EA7CBAF08651D714BDFBB,
//                is_yellow_year_vip=0, province=广东, screen_name=絮絮风飞, name=絮絮风飞,
//                iconurl=http://thirdqq.qlogo.cn/g?b=oidb&k=h7SaGY81hDe4ia7ndQvibSdw&s=100&t=1486951677,
//                yellow_vip_level=0, expiration=1670855391906, vip=0, expires_in=1670855391906}
                LoginReq req = new LoginReq();
                req.grantType = "weibo";
                req.authorizationCode = map.get("openid");
                API.login(req).watch(mActivity, new HttpCallback<LoginRes>() {
                    @Override
                    protected void onSuccess(LoginRes loginRes) {
                        UserCenter.setLoginRes(loginRes);
                        finish();
                        Intent intent = new Intent(mActivity, MainActivity.class);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                        startActivity(intent);
                    }
                });
            }

            @Override
            public void onError(SHARE_MEDIA share_media, int i, Throwable throwable) {
                Log.d(TAG, "onError() called with: share_media = [" + share_media + "], i = [" + i + "], throwable = [" + throwable + "]");

            }

            @Override
            public void onCancel(SHARE_MEDIA share_media, int i) {
                Log.d(TAG, "onCancel() called with: share_media = [" + share_media + "], i = [" + i + "]");
            }
        });
    }

    public void goPrivacy(View view) {
        start(PrivacyActivity.class);
    }

    public void goProtocol(View view) {
        start(UserProtocolActivity.class);
    }

    public void switchStatus(View view) {
        view.setSelected(!view.isSelected());
    }
}